# coding=utf-8
import logging
import random, requests, shutil, base64, hashlib, os
import time

from tests.nebula.api import NEBULA_API_TestCase
import lib



class TestNebulaApiPortraitsScenario108(NEBULA_API_TestCase):

    # def setUp(self, skip_wipe=False):
    #     pass

    @lib.test.Tag("ptest")
    def test_nebula_portraitsDb_crud(self):
        self.logTestTitle("人像库创建增删改查")
        db_name = 'white_%s' % self.getUuid(_len=5)
        db_type = 1  # 白名单
        db_capacity = 100
        description = 'auto_test_created'
        operator = 1024
        self.logTestStep('创建人像库')
        resp = self.portraitmanager_createPortraitDb(db_name=db_name, db_type=db_type, db_capacity=db_capacity,
                                                     description=description, operator=operator)
        self.assertEqual(resp.json_get("data.db_name"), db_name)
        self.assertEqual(resp.json_get("data.db_size"), 0)
        self.assertEqual(resp.json_get("data.db_capacity"), db_capacity)
        self.assertEqual(resp.json_get("data.db_type"), db_type)
        self.assertEqual(resp.json_get("data.description"), description)
        self.assertEqual(resp.json_get("data.created_user_code"), operator)
        self.assertEqual(resp.json_get("data.updated_user_code"), operator)
        self.assertEqual(resp.json_get("data.import_type"), 1)  # logging.conf 边, 2, 云
        # self.soft_assert(self.assertEqual, resp.json_get("data.feature_version"), 25100)  # feature_version默认25100
        db_id = resp.json_get("data.db_id")

        self.logTestStep('查询人像库')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id])
        self.assertEqual(resp.json_get("data.0.db_id"), db_id)
        self.assertEqual(resp.json_get("data.0.db_name"), db_name)
        self.assertEqual(resp.json_get("data.0.db_size"), 0)
        self.assertEqual(resp.json_get("data.0.db_capacity"), db_capacity)
        self.assertEqual(resp.json_get("data.0.db_type"), db_type)
        self.assertEqual(resp.json_get("data.0.import_type"), 1)  # logging.conf 边, 2, 云
        self.assertEqual(resp.json_get("data.0.description"), description)
        self.assertEqual(resp.json_get("data.0.created_user_code"), operator)
        self.assertEqual(resp.json_get("data.0.updated_user_code"), operator)
        self.assertEqual(resp.json_get("data.0.import_type"), 1)  # logging.conf 边, 2, 云
        # self.soft_assert(self.assertEqual, resp.json_get("data.0.feature_version"), 25000)  # feature_version默认25000

        self.logTestStep('更新人像库')
        new_db_name = "new_%s" % db_name
        new_db_type = 2
        new_db_capacity = 500
        new_operator = 648
        new_description = "new_desc"
        resp = self.portraitmanager_updatePortraitDb(db_id, db_name=new_db_name, db_type=new_db_type,
                                                     db_capacity=new_db_capacity, operator=new_operator, description=new_description)
        self.assertEqual(resp.json_get("data.db_name"), new_db_name)
        self.assertEqual(resp.json_get("data.db_size"), 0)
        self.assertEqual(resp.json_get("data.db_capacity"), new_db_capacity)
        self.assertEqual(resp.json_get("data.db_type"), new_db_type)
        self.assertEqual(resp.json_get("data.description"), new_description)
        self.assertEqual(resp.json_get("data.created_user_code"), operator)
        self.assertEqual(resp.json_get("data.updated_user_code"), new_operator)
        self.assertEqual(resp.json_get("data.import_type"), 1)  # logging.conf 边, 2, 云
        # self.soft_assert(self.assertEqual, resp.json_get("data.feature_version"), 25000)  # feature_version默认25000

        self.logTestStep('删除人像库')
        resp = self.portraitmanager_deletePortraitDb(db_id=db_id, unsafe_mode=True)
        self.assertEqual(resp.status_code, 200)

        self.logTestStep('再次查询人像库')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id])
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.json_get("data")), 0)

    def test_nebula_portraitsDb_dbSize(self):
        self.logTestTitle("人像库dbSize测试")
        db_name = 'white_%s' % self.getUuid(_len=5)
        db_type = 1  # 白名单
        db_capacity = 20
        description = 'auto_test_created'
        operator = 1024
        self.logTestStep('创建人像库')
        resp = self.portraitmanager_createPortraitDb(db_name=db_name, db_type=db_type, db_capacity=db_capacity,
                                                     description=description, operator=operator)
        db_id = resp.json_get("data.db_id")
        self.logTestStep('验证未推送图片前,人像库大小')
        self.assertEqual(resp.json_get("data.db_size"), 0)
        self.assertEqual(resp.json_get("data.db_capacity"), 20)

        self.logTestStep('创建人像20个,达到最大值')
        image_name1 = "xueqi1.jpg"
        image1 = self.imageToBase64(os.path.join(self.cfg.image_dir, image_name1))
        quality_threshold = None
        portrait_infos = []
        for x in range(db_capacity):
            portrait_infos.append({
                "name": "张三_%s" % x,
                "gender": 0,
                "age": 16,
                "phone": "13311236542",
                "company": "ST",
                "department": "SCG",
                "job_number": "12",
                "ic_number": "13",
                "id_number": "14",
                "remark": "autotest",
                "extra_info": "extra_info",
                "image": {
                    "data": image1,
                }
            })
        resp = self.portraitmanager_createPortraits(db_id=db_id, portrait_infos=portrait_infos,
                                                    quality_threshold=quality_threshold)
        portrait_ids = []
        for idx, info in enumerate(resp.json_get("details")):
            self.assertEqual(info["code"], 0, "第%s个人像创建失败" % idx)
            portrait_ids.append(info["portrait_id"])
        self.assertEqual(len(resp.json_get("details")), len(portrait_infos),
                         "返回的details数量与创建的人像数不符合%s:%s" % (len(resp.json_get("details")), len(portrait_infos)))

        self.logTestStep('创建人像后查询人像库db_size')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id])
        self.assertEqual(resp.json_get("data.0.db_id"), db_id)
        self.assertEqual(resp.json_get("data.0.db_size"), len(portrait_infos))

        # TODO 目前有bug 会返回500
        # self.logTestStep("当已经达到容量最大值后,不允许继续创建人像")
        # portrait_infos = [
        #     {
        #         "name": "张三",
        #         "gender": 0,
        #         "age": 16,
        #         "phone": "13311236542",
        #         "company": "ST",
        #         "department": "SCG",
        #         "job_number": "12",
        #         "ic_number": "13",
        #         "id_number": "14",
        #         "remark": "autotest",
        #         "extra_info": "extra_info",
        #         "image": {
        #             "data": image1,
        #         }
        #     }
        # ]
        # resp = self.portraitmanager_createPortraits(db_id=db_id, portrait_infos=portrait_infos,
        #                                             quality_threshold=quality_threshold)

        self.logTestStep('删除人像')
        delete_portrait_id = portrait_ids[-1]
        resp = self.portraitmanager_deletePortraits(db_id=db_id, portrait_ids=[delete_portrait_id])
        self.assertEqual(resp.json_get("details.0.code"), 0)
        self.assertEqual(resp.json_get("details.0.portrait_id"), delete_portrait_id)

        self.logTestStep('删除后再次查询人像')
        resp = self.portraitmanager_queryPortraits(db_id=db_id, portrait_ids=portrait_ids)
        self.assertEqual(len(resp.json_get("details")), len(portrait_infos) - 1)

        self.logTestStep('删除人像后查询人像库db_size')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id])
        self.assertEqual(resp.json_get("data.0.db_id"), db_id)
        self.assertEqual(resp.json_get("data.0.db_size"), len(portrait_infos) - 1)

        self.logTestStep('再次创建一张人像,预期成功')
        portrait_infos = [
            {
                "name": "张三",
                "gender": 0,
                "age": 16,
                "phone": "13311236542",
                "company": "ST",
                "department": "SCG",
                "job_number": "12",
                "ic_number": "13",
                "id_number": "14",
                "remark": "autotest",
                "extra_info": "extra_info",
                "image": {
                    "data": image1,
                }
            }
        ]
        resp = self.portraitmanager_createPortraits(db_id=db_id, portrait_infos=portrait_infos,
                                                    quality_threshold=quality_threshold)
        self.assertEqual(resp.json_get("details.0.code"), 0)

        self.logTestStep('删除人像库')
        resp = self.portraitmanager_deletePortraitDb(db_id=db_id, unsafe_mode=True)
        self.assertEqual(resp.status_code, 200)

        self.logTestStep('再次查询人像库,确认删除成功')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id])
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.json_get("data")), 0)

    def test_nebula_portraits_crud(self):
        self.logTestTitle("人像创建增删改查")
        db_name = 'white_%s' % self.getUuid(_len=5)
        db_type = 1  # 白名单
        db_capacity = 100
        description = 'auto_test_created'
        operator = 1024
        self.logTestStep('创建人像库')
        resp = self.portraitmanager_createPortraitDb(db_name=db_name, db_type=db_type, db_capacity=db_capacity,
                                                     description=description, operator=operator)
        db_id = resp.json_get("data.db_id")
        self.logTestStep('验证未推送图片前,人像库大小')
        self.assertEqual(resp.json_get("data.db_size"), 0)

        self.logTestStep('创建人像')
        image_name1 = "xueqi1.jpg"
        image1 = self.imageToBase64(os.path.join(self.cfg.image_dir, image_name1))
        quality_threshold = None
        portrait_infos = [
            {
              "name": "张三",
              "gender": 0,
              "age": 16,
              "phone": "13311236542",
              "company": "ST",
              "department": "SCG",
              "job_number": "12",
              "ic_number": "13",
              "id_number": "14",
              "remark": "autotest",
              "extra_info": "extra_info",
              "image": {
                "data": image1,
              }
            }
        ]
        resp = self.portraitmanager_createPortraits(db_id=db_id, portrait_infos=portrait_infos, quality_threshold=quality_threshold)
        self.assertEqual(resp.json_get("details.0.code"), 0)
        portrait_id = resp.json_get("details.0.portrait_id")

        portrait_ids = [portrait_id]
        resp = self.portraitmanager_queryPortraits(db_id=db_id, portrait_ids=portrait_ids)
        self.assertEqual(resp.json_get("details.0.portrait_info.portrait_id"), portrait_id)
        self.assertEqual(resp.json_get("details.0.portrait_info.name"), portrait_infos[0]["name"])
        self.assertEqual(resp.json_get("details.0.portrait_info.gender"), portrait_infos[0]["gender"])
        self.assertEqual(resp.json_get("details.0.portrait_info.phone"), portrait_infos[0]["phone"])
        self.assertEqual(resp.json_get("details.0.portrait_info.company"), portrait_infos[0]["company"])
        self.assertEqual(resp.json_get("details.0.portrait_info.department"), portrait_infos[0]["department"])
        self.assertEqual(resp.json_get("details.0.portrait_info.job_number"), portrait_infos[0]["job_number"])
        self.assertEqual(resp.json_get("details.0.portrait_info.ic_number"), portrait_infos[0]["ic_number"])
        self.assertEqual(resp.json_get("details.0.portrait_info.id_number"), portrait_infos[0]["id_number"])
        self.assertEqual(resp.json_get("details.0.portrait_info.remark"), portrait_infos[0]["remark"])
        self.assertEqual(resp.json_get("details.0.portrait_info.extra_info"), portrait_infos[0]["extra_info"])
        self.assertTrue(resp.json_get("details.0.portrait_info.image.url"))
        url = resp.json_get("details.0.portrait_info.image.url")
        abs_url = "http://%s:8158/_/%s" % (self.cfg.ip, url)
        save_path = "/tmp/%s" % image_name1
        if os.path.exists(save_path):
            os.remove(save_path)
        self.download_file(abs_url, save_path)
        self.assertTrue(os.path.isfile(save_path), '[%s]图片下载失败. url:%s' % (image_name1, save_path))
        origin_md5 = self.getMd5ByFile(os.path.join(self.cfg.image_dir, image_name1))
        download_md5 = self.getMd5ByFile(save_path)
        self.assertEqual(download_md5, origin_md5, "创建后,上传图片与原图片不一致.path:%s" % save_path)

        self.logTestStep('更新人像')
        image_name2 = "xueqi2.jpg"
        image2 = self.imageToBase64(os.path.join(self.cfg.image_dir, image_name2))
        update_portrait_infos = [
            {
                "name": "张三新",
                "gender": 1,
                "age": 26,
                "phone": "13311236577",
                "company": "ST大学",
                "department": "大三一班",
                "job_number": "22",
                "ic_number": "33",
                "id_number": "44",
                "remark": "autotest_update",
                "extra_info": "extra_info_update",
                "portrait_id": portrait_id,
                "image": {
                    "data": image2,
                }
            },
        ]
        resp = self.portraitmanager_updatePortraits(db_id=db_id, portrait_infos=update_portrait_infos)
        self.assertEqual(resp.json_get("details.0.code"), 0)
        self.assertEqual(resp.json_get("details.0.portrait_id"), portrait_id)

        self.logTestStep('查询更新后的人像')
        update_portrait_info = update_portrait_infos[0]
        resp = self.portraitmanager_queryPortraits(db_id=db_id, portrait_ids=portrait_ids)
        self.assertEqual(resp.json_get("details.0.portrait_info.portrait_id"), portrait_id)
        self.assertEqual(resp.json_get("details.0.portrait_info.name"), update_portrait_info["name"])
        self.assertEqual(resp.json_get("details.0.portrait_info.gender"), update_portrait_info["gender"])
        self.assertEqual(resp.json_get("details.0.portrait_info.phone"), update_portrait_info["phone"])
        self.assertEqual(resp.json_get("details.0.portrait_info.company"), update_portrait_info["company"])
        self.assertEqual(resp.json_get("details.0.portrait_info.department"), update_portrait_info["department"])
        self.assertEqual(resp.json_get("details.0.portrait_info.job_number"), update_portrait_info["job_number"])
        self.assertEqual(resp.json_get("details.0.portrait_info.ic_number"), update_portrait_info["ic_number"])
        self.assertEqual(resp.json_get("details.0.portrait_info.id_number"), update_portrait_info["id_number"])
        self.assertEqual(resp.json_get("details.0.portrait_info.remark"), update_portrait_info["remark"])
        self.assertEqual(resp.json_get("details.0.portrait_info.extra_info"), update_portrait_info["extra_info"])
        self.assertTrue(resp.json_get("details.0.portrait_info.image.url"))
        url = resp.json_get("details.0.portrait_info.image.url")
        abs_url = "http://%s:8158/_/%s" % (self.cfg.ip, url)
        save_path = "/tmp/%s" % image_name2
        if os.path.exists(save_path):
            os.remove(save_path)
        self.download_file(abs_url, save_path)
        self.assertTrue(os.path.isfile(save_path), '[%s]图片下载失败. url:%s' % (image_name2,save_path))
        origin_md5 = self.getMd5ByFile(os.path.join(self.cfg.image_dir, image_name2))
        download_md5 = self.getMd5ByFile(save_path)
        self.assertEqual(download_md5, origin_md5, "更新后, 上传图片与原图片不一致,path:%s" % save_path)

        self.logTestStep('删除人像')
        resp = self.portraitmanager_deletePortraits(db_id=db_id, portrait_ids=portrait_ids)
        self.assertEqual(resp.json_get("details.0.code"), 0)
        self.assertEqual(resp.json_get("details.0.portrait_id"), portrait_id)

        self.logTestStep('删除后再次查询人像')
        resp = self.portraitmanager_queryPortraits(db_id=db_id, portrait_ids=portrait_ids)
        self.assertEqual(len(resp.json_get("details")), 0)

        self.logTestStep('删除人像库')
        resp = self.portraitmanager_deletePortraitDb(db_id=db_id, unsafe_mode=True)
        self.assertEqual(resp.status_code, 200)

        self.logTestStep('再次查询人像库,确认删除成功')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id])
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.json_get("data")), 0)

        self.logTestStep('在已删除的人像库里查询人像')
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id], page={"offset": 0, "limit": 10, "total": 0})
        self.assertEqual(resp.status_code, 400)

    def test_nebula_portraits_crud_batch(self):
        self.logTestTitle("人像批量创建增删改查")
        db_name = 'white_%s' % self.getUuid(_len=5)
        db_type = 1  # 白名单
        db_capacity = 100
        description = 'auto_test_created'
        operator = 1024
        self.logTestStep('创建人像库')
        resp = self.portraitmanager_createPortraitDb(db_name=db_name, db_type=db_type, db_capacity=db_capacity,
                                                     description=description, operator=operator)
        db_id = resp.json_get("data.db_id")
        self.logTestStep('验证未推送图片前,人像库大小')
        self.assertEqual(resp.json_get("data.db_size"), 0)

        self.logTestStep('创建人像')
        image_name1 = "xueqi1.jpg"
        image1 = self.imageToBase64(os.path.join(self.cfg.image_dir, image_name1))
        portrait_infos = []
        batch_num = 10
        for x in range(batch_num):
            portrait_infos.append({
                    "name": "张三%s" % x,
                    "gender": 0,
                    "age": 16,
                    "phone": "13311236542",
                    "company": "ST",
                    "department": "SCG",
                    "job_number": "%s" % x,
                    "ic_number": "%s" % x,
                    "id_number": "%s" % x,
                    "remark": "autotest",
                    "extra_info": "extra_info",
                    "image": {
                        "data": image1,
                    }
                }
            )
        resp = self.portraitmanager_createPortraits(db_id=db_id, portrait_infos=portrait_infos)
        portrait_ids = []
        for idx, info in enumerate(resp.json_get("details")):
            self.assertEqual(info["code"], 0, "第%s个error, code=%s" % (idx, info["code"]))
            portrait_ids.append(info["portrait_id"])

        self.logTestStep('创建人像后查询人像库db_size')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id])
        self.assertEqual(resp.json_get("data.0.db_id"), db_id)
        self.assertEqual(resp.json_get("data.0.db_name"), db_name)
        self.assertEqual(resp.json_get("data.0.db_size"), batch_num)

        origin_md5 = self.getMd5ByFile(os.path.join(self.cfg.image_dir, image_name1))
        resp = self.portraitmanager_queryPortraits(db_id=db_id, portrait_ids=portrait_ids)
        portrait_infos_dict = {}
        for idx, info in enumerate(resp.json_get("details")):
            self.assertTrue(info["portrait_info"]["portrait_id"] in portrait_ids, "第%s个error, portrait_id=%s" % (idx, info["portrait_info"]["portrait_id"]))
            portrait_infos_dict[info["portrait_info"]["portrait_id"]] = info["portrait_info"]
            self.assertEqual(info['portrait_info']["name"], portrait_infos[idx]["name"], "第%s个error, name=%s" % (idx, info['portrait_info']["name"]))
            self.assertEqual(info['portrait_info']["gender"], portrait_infos[idx]["gender"], "第%s个error, gender=%s" % (idx, info['portrait_info']["gender"]))
            self.assertEqual(info['portrait_info']["phone"], portrait_infos[idx]["phone"], "第%s个error, phone=%s" % (idx, info['portrait_info']["phone"]))
            self.assertEqual(info['portrait_info']["company"], portrait_infos[idx]["company"], "第%s个error, company=%s" % (idx, info['portrait_info']["company"]))
            self.assertEqual(info['portrait_info']["department"], portrait_infos[idx]["department"], "第%s个error, department=%s" % (idx, info['portrait_info']["department"]))
            self.assertEqual(info['portrait_info']["job_number"], portrait_infos[idx]["job_number"], "第%s个error, job_number=%s" % (idx, info['portrait_info']["job_number"]))
            self.assertEqual(info['portrait_info']["ic_number"], portrait_infos[idx]["ic_number"], "第%s个error, ic_number=%s" % (idx, info['portrait_info']["ic_number"]))
            self.assertEqual(info['portrait_info']["id_number"], portrait_infos[idx]["id_number"], "第%s个error, id_number=%s" % (idx, info['portrait_info']["id_number"]))
            self.assertEqual(info['portrait_info']["remark"], portrait_infos[idx]["remark"], "第%s个error, remark=%s" % (idx, info['portrait_info']["remark"]))
            self.assertEqual(info['portrait_info']["extra_info"], portrait_infos[idx]["extra_info"], "第%s个error, extra_info=%s" % (idx, info['portrait_info']["extra_info"]))
            self.assertTrue(info["portrait_info"]["image"]["url"], "第%s个error, url=%s" % (idx, info["portrait_info"]["image"]["url"]))
            url = info["portrait_info"]["image"]["url"]
            abs_url = "http://%s:8158/_/%s" % (self.cfg.ip, url)
            save_path = "/tmp/%s" % image_name1
            if os.path.exists(save_path):
                os.remove(save_path)
            self.download_file(abs_url, save_path)
            self.assertTrue(os.path.isfile(save_path), '[%s]图片下载失败. url:%s' % (image_name1, save_path))
            download_md5 = self.getMd5ByFile(save_path)
            self.assertEqual(download_md5, origin_md5, "创建后,上传图片与原图片不一致.path:%s" % save_path)

        self.logTestStep('更新人像')
        image_name2 = "xueqi2.jpg"
        image2 = self.imageToBase64(os.path.join(self.cfg.image_dir, image_name2))
        portraits = []
        update_dict = {}
        for portrait_id, info in portrait_infos_dict.items():
            new_info = {
                           "portrait_id": portrait_id,
                           "name": "new_%s" % info["name"],
                           "gender": 1,
                           "age": 26,
                           "phone": "13311236577",
                           "company": "ST大学",
                           "department": "大三一班",
                           "job_number": "22",
                           "ic_number": "33",
                           "id_number": "44",
                           "remark": "autotest_update",
                           "extra_info": "extra_info_update",
                           "image": {"data": image2}
            }
            portraits.append(new_info)
            update_dict[portrait_id] = new_info

        resp = self.portraitmanager_updatePortraits(db_id=db_id, portrait_infos=portraits)
        for idx, info in enumerate(resp.json_get("details")):
            self.assertEqual(info["code"], 0, "更新 第%s个error, code=%s" % (idx, info["code"]))

        self.logTestStep('查询更新后的人像')
        origin_md5 = self.getMd5ByFile(os.path.join(self.cfg.image_dir, image_name2))
        resp = self.portraitmanager_queryPortraits(db_id=db_id, portrait_ids=portrait_ids)
        for idx, info in enumerate(resp.json_get("details")):
            self.assertTrue(info['portrait_info']["portrait_id"] in portrait_ids, "第%s个error, portrait_id=%s" % (idx, info['portrait_info']["portrait_id"]))
            portrait_id = info['portrait_info']["portrait_id"]
            self.assertEqual(info['portrait_info']["name"], update_dict[portrait_id]["name"], "第%s个error, name=%s" % (idx, info['portrait_info']["name"]))
            self.assertEqual(info['portrait_info']["gender"], update_dict[portrait_id]["gender"], "第%s个error, gender=%s" % (idx, info['portrait_info']["gender"]))
            self.assertEqual(info['portrait_info']["phone"], update_dict[portrait_id]["phone"], "第%s个error, phone=%s" % (idx, info['portrait_info']["phone"]))
            self.assertEqual(info['portrait_info']["company"], update_dict[portrait_id]["company"], "第%s个error, company=%s" % (idx, info['portrait_info']["company"]))
            self.assertEqual(info['portrait_info']["department"], update_dict[portrait_id]["department"], "第%s个error, department=%s" % (idx, info['portrait_info']["department"]))
            self.assertEqual(info['portrait_info']["job_number"], update_dict[portrait_id]["job_number"], "第%s个error, job_number=%s" % (idx, info['portrait_info']["job_number"]))
            self.assertEqual(info['portrait_info']["ic_number"], update_dict[portrait_id]["ic_number"], "第%s个error, ic_number=%s" % (idx, info['portrait_info']["ic_number"]))
            self.assertEqual(info['portrait_info']["id_number"], update_dict[portrait_id]["id_number"], "第%s个error, id_number=%s" % (idx, info['portrait_info']["id_number"]))
            self.assertEqual(info['portrait_info']["remark"], update_dict[portrait_id]["remark"], "第%s个error, remark=%s" % (idx, info['portrait_info']["remark"]))
            self.assertEqual(info['portrait_info']["extra_info"], update_dict[portrait_id]["extra_info"], "第%s个error, extra_info=%s" % (idx, info['portrait_info']["extra_info"]))
            self.assertTrue(info["portrait_info"]["image"]["url"], "第%s个error, url=%s" % (idx, info["portrait_info"]["image"]["url"]))
            url = resp.json_get("details.0.portrait_info.image.url")
            abs_url = "http://%s:8158/_/%s" % (self.cfg.ip, url)
            save_path = "/tmp/%s" % image_name2
            if os.path.exists(save_path):
                os.remove(save_path)
            self.download_file(abs_url, save_path)
            self.assertTrue(os.path.isfile(save_path), '[%s]图片下载失败. url:%s' % (image_name2, save_path))
            download_md5 = self.getMd5ByFile(save_path)
            self.assertEqual(download_md5, origin_md5, "更新后, 上传图片与原图片不一致,path:%s" % save_path)

        self.logTestStep('删除人像')
        resp = self.portraitmanager_deletePortraits(db_id=db_id, portrait_ids=portrait_ids)
        for idx, info in enumerate(resp.json_get("details")):
            self.assertEqual(info["code"], 0, "更新 第%s个error, code=%s" % (idx, info["code"]))
        self.logTestStep('删除后再次查询人像')

        resp = self.portraitmanager_queryPortraits(db_id=db_id, portrait_ids=portrait_ids)

        self.assertEqual(len(resp.json_get("details")), 0)
        self.logTestStep('删除人像后查询人像库db_size')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id])
        self.assertEqual(resp.json_get("data.0.db_id"), db_id)
        self.assertEqual(resp.json_get("data.0.db_name"), db_name)
        self.assertEqual(resp.json_get("data.0.db_size"), 0)

        self.logTestStep('删除人像库')
        resp = self.portraitmanager_deletePortraitDb(db_id=db_id, unsafe_mode=True)
        self.assertEqual(resp.status_code, 200)

        self.logTestStep('再次查询人像库,确认删除成功')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id])
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.json_get("data")), 0)

    def test_nebula_portraits_portraitList(self):
        self.logTestTitle("查询人像列表")
        db_name = 'white_%s' % self.getUuid(_len=5)
        db_type = 1  # 白名单
        db_capacity = 100
        description = 'auto_test_created'
        operator = 1024
        self.logTestStep('创建人像库1')
        resp = self.portraitmanager_createPortraitDb(db_name=db_name, db_type=db_type, db_capacity=db_capacity,
                                                     description=description, operator=operator)
        db_id1 = resp.json_get("data.db_id")

        self.logTestStep('在db1创建人像10张')
        image_name_list = ["guojin1.jpg", "guojin2.jpg", "pengcheng1.jpg", "pengcheng2.jpg",
                           "shihan1.jpg", "shihan2.jpg", "shihan3.jpg", "shihan4.jpg", "xueqi1.jpg", "xueqi2.jpg"]
        portrait_infos = []
        for x in range(len(image_name_list)):
            image1 = self.imageToBase64(os.path.join(self.cfg.image_dir, image_name_list[x]))
            portrait_infos.append({
                "name": image_name_list[x].split(".")[0],
                "gender": 0,
                "age": 16,
                "phone": "%s" % x,
                "company": "%s" % x,
                "department": "%s" % x,
                "job_number": "%s" % x,
                "ic_number": "%s" % x,
                "id_number": "%s" % x,
                "remark": "autotest",
                "extra_info": "extra_info",
                "image": {
                    "data": image1,
                }
            })
        resp = self.portraitmanager_createPortraits(db_id=db_id1, portrait_infos=portrait_infos)
        portrait_ids = []
        for idx, info in enumerate(resp.json_get("details")):
            self.assertEqual(info["code"], 0, "第%s个error, code=%s" % (idx, info["code"]))
            portrait_ids.append(info["portrait_id"])

        self.logTestStep('测试encrypted = True')
        resp = self.portraitmanager_listPortraits(db_id=db_id1, encrypted=True, page_offset=0, page_limit=20)
        self.assertTrue(resp.json_get("data.portraits.0.portrait_info.job_number") != "0")
        self.logTestStep('测试encrypted = False')
        resp = self.portraitmanager_listPortraits(db_id=db_id1, encrypted=False, page_offset=0, page_limit=20)
        self.assertTrue(resp.json_get("data.portraits.0.portrait_info.job_number") == "0")
        self.logTestStep('测试分页')
        resp = self.portraitmanager_listPortraits(db_id=db_id1, encrypted=False, page_offset=0, page_limit=2)
        self.assertTrue(len(resp.json_get("data.portraits")) == 2)
        resp = self.portraitmanager_listPortraits(db_id=db_id1, encrypted=False, page_offset=9, page_limit=10)
        self.assertTrue(len(resp.json_get("data.portraits")) == 1)

        self.logTestStep('测试默认分页')
        # TODO　目前有bug　不填写分页，会返回参数错误
        resp = self.portraitmanager_listPortraits(db_id=db_id1, encrypted=False)

        self.logTestStep('删除人像库')
        resp = self.portraitmanager_deletePortraitDb(db_id=db_id1, unsafe_mode=True)
        self.assertEqual(resp.status_code, 200)
        self.logTestStep('再次查询人像库,确认删除成功')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id1])
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.json_get("data")), 0)

    def test_nebula_portraits_query_portraits(self):
        self.logTestTitle("查询人像")
        db_name = 'white_%s' % self.getUuid(_len=5)
        db_type = 1  # 白名单
        db_capacity = 100
        description = 'auto_test_created'
        operator = 1024
        self.logTestStep('创建人像库1')
        resp = self.portraitmanager_createPortraitDb(db_name=db_name, db_type=db_type, db_capacity=db_capacity,
                                                     description=description, operator=operator)
        db_id1 = resp.json_get("data.db_id")

        self.logTestStep('在db1创建人像10张')
        image_name_list = ["guojin1.jpg", "guojin2.jpg", "pengcheng1.jpg", "pengcheng2.jpg",
                           "shihan1.jpg", "shihan2.jpg", "shihan3.jpg", "shihan4.jpg", "xueqi1.jpg", "xueqi2.jpg"]
        portrait_infos = []
        for x in range(len(image_name_list)):
            image1 = self.imageToBase64(os.path.join(self.cfg.image_dir, image_name_list[x]))
            portrait_infos.append({
                "name": image_name_list[x].split(".")[0],
                "gender": 0,
                "age": 16,
                "phone": "%s" % x,
                "company": "%s" % x,
                "department": "%s" % x,
                "job_number": "%s" % x,
                "ic_number": "%s" % x,
                "id_number": "%s" % x,
                "remark": "autotest",
                "extra_info": "extra_info",
                "image": {
                    "data": image1,
                }
            })
        resp = self.portraitmanager_createPortraits(db_id=db_id1, portrait_infos=portrait_infos)
        portrait_ids = []
        for idx, info in enumerate(resp.json_get("details")):
            self.assertEqual(info["code"], 0, "第%s个error, code=%s" % (idx, info["code"]))
            portrait_ids.append(info["portrait_id"])

        self.logTestStep('测试encrypted = True')
        resp = self.portraitmanager_queryPortraits(db_id=db_id1, encrypted=True, portrait_ids=[portrait_ids[0]])
        self.assertTrue(resp.json_get("details.0.portrait_info.job_number") != "0")
        self.logTestStep('测试encrypted = False')
        resp = self.portraitmanager_queryPortraits(db_id=db_id1, encrypted=False, portrait_ids=[portrait_ids[0]])
        self.assertTrue(resp.json_get("details.0.portrait_info.job_number") == "0")

        self.logTestStep('删除人像库')
        resp = self.portraitmanager_deletePortraitDb(db_id=db_id1, unsafe_mode=True)
        self.assertEqual(resp.status_code, 200)
        self.logTestStep('再次查询人像库,确认删除成功')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id1])
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.json_get("data")), 0)


    def test_nebula_portraits_search_portrait(self):
        self.logTestTitle("使用ic,id jobnumber搜索人像")
        db_name = 'white_%s' % self.getUuid(_len=5)
        db_type = 1  # 白名单
        db_capacity = 100
        description = 'auto_test_created'
        operator = 1024
        self.logTestStep('创建人像库1')
        resp = self.portraitmanager_createPortraitDb(db_name=db_name, db_type=db_type, db_capacity=db_capacity,
                                                     description=description, operator=operator)
        db_id1 = resp.json_get("data.db_id")

        db_name = 'black_%s' % self.getUuid(_len=5)
        db_type = 2  # 白名单
        db_capacity = 100
        description = 'auto_test_created'
        operator = 1024
        self.logTestStep('创建人像库2')
        resp = self.portraitmanager_createPortraitDb(db_name=db_name, db_type=db_type, db_capacity=db_capacity,
                                                     description=description, operator=operator)
        db_id2 = resp.json_get("data.db_id")

        self.logTestStep('在db1创建人像张三1-10')
        image_name1 = "xueqi1.jpg"
        image1 = self.imageToBase64(os.path.join(self.cfg.image_dir, image_name1))
        portrait_infos = []
        batch_num = 10
        for x in range(batch_num):
            portrait_infos.append({
                "name": "张三%s" % x,
                "gender": 0,
                "age": 16,
                "phone": "%s" % x,
                "company": "%s" % x,
                "department": "%s" % x,
                "job_number": "%s" % x,
                "ic_number": "%s" % x,
                "id_number": "%s" % x,
                "remark": "autotest",
                "extra_info": "extra_info",
                "image": {
                    "data": image1,
                }
            }
            )
        resp = self.portraitmanager_createPortraits(db_id=db_id2, portrait_infos=portrait_infos)
        portrait_ids = []
        for idx, info in enumerate(resp.json_get("details")):
            self.assertEqual(info["code"], 0, "第%s个error, code=%s" % (idx, info["code"]))
            portrait_ids.append(info["portrait_id"])

        self.logTestStep('在db2创建人像李四1-10')
        image_name2 = "xueqi2.jpg"
        image2 = self.imageToBase64(os.path.join(self.cfg.image_dir, image_name2))
        portrait_infos2 = []
        batch_num = 10
        for x in range(batch_num):
            portrait_infos2.append({
                "name": "李四%s" % x,
                "gender": 0,
                "age": 16,
                "phone": "%s" % x,
                "company": "%s" % x,
                "department": "%s" % x,
                "job_number": "%s" % x,
                "ic_number": "%s" % x,
                "id_number": "%s" % x,
                "remark": "autotest",
                "extra_info": "extra_info",
                "image": {
                    "data": image2,
                }
            }
            )
        resp = self.portraitmanager_createPortraits(db_id=db_id2, portrait_infos=portrait_infos2)
        portrait_ids2 = []
        for idx, info in enumerate(resp.json_get("details")):
            self.assertEqual(info["code"], 0, "第%s个error, code=%s" % (idx, info["code"]))
            portrait_ids2.append(info["portrait_id"])

        self.logTestStep('在两个库按名字搜索人像')
        page = {"offset": 0, "limit": 50}
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, name="张三3")
        self.assertEqual(len(resp.json_get("details.portraits")), 1)
        self.assertEqual(resp.json_get("details.portraits.0.portrait_info.name"), "张三3")
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, name="李四5")
        self.assertEqual(len(resp.json_get("details.portraits")), 1)
        self.assertEqual(resp.json_get("details.portraits.0.portrait_info.name"), "李四5")
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, name="王五")
        self.assertEqual(len(resp.json_get("details.portraits")), 0)
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1], page=page, name="李四5")
        self.assertEqual(len(resp.json_get("details.portraits")), 0)

        self.logTestStep('在两个库按工号搜索人像')
        page = {"offset": 0, "limit": 50}
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, job_number="3")
        self.assertEqual(len(resp.json_get("details.portraits")), 2)
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, job_number="5")
        self.assertEqual(len(resp.json_get("details.portraits")), 2)
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, job_number="52")
        self.assertEqual(len(resp.json_get("details.portraits")), 0)

        self.logTestStep('在两个库按IC卡号搜索人像')
        page = {"offset": 0, "limit": 50}
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, ic_number="3")
        self.assertEqual(len(resp.json_get("details.portraits")), 2)
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, ic_number="5")
        self.assertEqual(len(resp.json_get("details.portraits")), 2)
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, ic_number="52")
        self.assertEqual(len(resp.json_get("details.portraits")), 0)

        self.logTestStep('在两个库按身份证号搜索人像')
        page = {"offset": 0, "limit": 50}
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, id_number="3")
        self.assertEqual(len(resp.json_get("details.portraits")), 2)
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, id_number="5")
        self.assertEqual(len(resp.json_get("details.portraits")), 2)
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, id_number="52")
        self.assertEqual(len(resp.json_get("details.portraits")), 0)

        self.logTestStep('在两个库指定所有参数搜索人像')
        page = {"offset": 0, "limit": 50}
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, name="张三3", job_number="3",
                                                    ic_number="3", id_number="3")
        self.assertEqual(len(resp.json_get("details.portraits")), 1)
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, name="李四3", job_number="3",
                                                    ic_number="3", id_number="3")
        self.assertEqual(len(resp.json_get("details.portraits")), 1)
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1], page=page, name="李四3", job_number="3",
                                                    ic_number="3", id_number="3")
        self.assertEqual(len(resp.json_get("details.portraits")), 0)
        resp = self.portraitmanager_searchPortraits(db_ids=[db_id1, db_id2], page=page, name="李四3", job_number="30",
                                                    ic_number="3", id_number="3")
        self.assertEqual(len(resp.json_get("details.portraits")), 0)
        self.logTestStep('删除人像库')
        resp = self.portraitmanager_deletePortraitDb(db_id=db_id1, unsafe_mode=True)
        self.assertEqual(resp.status_code, 200)
        resp = self.portraitmanager_deletePortraitDb(db_id=db_id2, unsafe_mode=True)
        self.assertEqual(resp.status_code, 200)
        self.logTestStep('再次查询人像库,确认删除成功')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id1])
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.json_get("data")), 0)
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id2])
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.json_get("data")), 0)

    def test_nebula_portraits_search_portrait_1VsN(self):
        self.logTestTitle("在多个库进行1:N对比")
        db_name = 'white_%s' % self.getUuid(_len=5)
        db_type = 1  # 白名单
        db_capacity = 100
        description = 'auto_test_created'
        operator = 1024
        self.logTestStep('创建人像库1')
        resp = self.portraitmanager_createPortraitDb(db_name=db_name, db_type=db_type, db_capacity=db_capacity,
                                                     description=description, operator=operator)
        db_id1 = resp.json_get("data.db_id")

        db_name = 'black_%s' % self.getUuid(_len=5)
        db_type = 2  # 白名单
        db_capacity = 100
        description = 'auto_test_created'
        operator = 1024
        self.logTestStep('创建人像库2')
        resp = self.portraitmanager_createPortraitDb(db_name=db_name, db_type=db_type, db_capacity=db_capacity,
                                                     description=description, operator=operator)
        db_id2 = resp.json_get("data.db_id")
        self.logTestStep('在db1创建人像10张')
        image_name_list = ["guojin1.jpg", "guojin2.jpg", "pengcheng1.jpg", "pengcheng2.jpg",
                           "shihan1.jpg", "shihan2.jpg", "shihan3.jpg", "shihan4.jpg", "xueqi1.jpg", "xueqi2.jpg"]
        portrait_infos = []
        for x in range(len(image_name_list)):
            image1 = self.imageToBase64(os.path.join(self.cfg.image_dir, image_name_list[x]))
            portrait_infos.append({
                "name": image_name_list[x].split(".")[0],
                "gender": 0,
                "age": 16,
                "phone": "%s" % x,
                "company": "%s" % x,
                "department": "%s" % x,
                "job_number": "%s" % x,
                "ic_number": "%s" % x,
                "id_number": "%s" % x,
                "remark": "autotest",
                "extra_info": "extra_info",
                "image": {
                    "data": image1,
                }
            })
        resp = self.portraitmanager_createPortraits(db_id=db_id1, portrait_infos=portrait_infos)
        portrait_ids = []
        for idx, info in enumerate(resp.json_get("details")):
            self.assertEqual(info["code"], 0, "第%s个error, code=%s" % (idx, info["code"]))
            portrait_ids.append(info["portrait_id"])

        self.logTestStep('在db2创建人像10张')
        portrait_infos2 = []
        for x in range(len(image_name_list)):
            image1 = self.imageToBase64(os.path.join(self.cfg.image_dir, image_name_list[x]))
            portrait_infos2.append({
                "name": image_name_list[x].split(".")[0],
                "gender": 0,
                "age": 16,
                "phone": "%s" % x,
                "company": "%s" % x,
                "department": "%s" % x,
                "job_number": "%s" % x,
                "ic_number": "%s" % x,
                "id_number": "%s" % x,
                "remark": "autotest",
                "extra_info": "extra_info",
                "image": {
                    "data": image1,
                }
            })
        resp = self.portraitmanager_createPortraits(db_id=db_id2, portrait_infos=portrait_infos2)
        portrait_ids2 = []
        for idx, info in enumerate(resp.json_get("details")):
            self.assertEqual(info["code"], 0, "第%s个error, code=%s" % (idx, info["code"]))
            portrait_ids2.append(info["portrait_id"])


        self.logTestStep('db1,db2中1:N对比')
        dbs = [
            {"db_id": db_id1, "topk": 5, "min_score": 0},
            {"db_id": db_id1, "topk": 20, "min_score": 0},
            {"db_id": db_id2, "topk": 5, "min_score": 0.9},
            {"db_id": db_id2, "topk": 5, "min_score": 0.01},
        ]
        image = {"data": self.imageToBase64(os.path.join(self.cfg.image_dir, "xueqi1.jpg"))}
        resp = self.portraitmanager_searchImageInDBs(dbs=dbs, image=image)
        self.assertEqual(len(resp.json_get("details.0.similar_results")), 5)
        self.assertEqual(len(resp.json_get("details.logging.conf.similar_results")), 10)
        self.assertEqual(len(resp.json_get("details.2.similar_results")), 2)
        self.assertEqual(len(resp.json_get("details.3.similar_results")), 3)

        self.logTestStep('删除人像库')
        resp = self.portraitmanager_deletePortraitDb(db_id=db_id1, unsafe_mode=True)
        self.assertEqual(resp.status_code, 200)
        resp = self.portraitmanager_deletePortraitDb(db_id=db_id2, unsafe_mode=True)
        self.assertEqual(resp.status_code, 200)
        self.logTestStep('再次查询人像库,确认删除成功')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id1])
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.json_get("data")), 0)
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id2])
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.json_get("data")), 0)

    def test_nebula_portraits_search_portrait_1Vs1(self):
        self.logTestTitle("单张图片跟库中指定人像进行1:1比对")
        db_name = 'white_%s' % self.getUuid(_len=5)
        db_type = 1  # 白名单
        db_capacity = 100
        description = 'auto_test_created'
        operator = 1024
        self.logTestStep('创建人像库1')
        resp = self.portraitmanager_createPortraitDb(db_name=db_name, db_type=db_type, db_capacity=db_capacity,
                                                     description=description, operator=operator)
        db_id1 = resp.json_get("data.db_id")

        self.logTestStep('在db1创建人像10张')
        image_name_list = ["guojin1.jpg", "guojin2.jpg", "pengcheng1.jpg", "pengcheng2.jpg",
                           "shihan1.jpg", "shihan2.jpg", "shihan3.jpg", "shihan4.jpg", "xueqi1.jpg", "xueqi2.jpg"]
        portrait_infos = []
        for x in range(len(image_name_list)):
            image1 = self.imageToBase64(os.path.join(self.cfg.image_dir, image_name_list[x]))
            portrait_infos.append({
                "name": image_name_list[x].split(".")[0],
                "gender": 0,
                "age": 16,
                "phone": "%s" % x,
                "company": "%s" % x,
                "department": "%s" % x,
                "job_number": "%s" % x,
                "ic_number": "%s" % x,
                "id_number": "%s" % x,
                "remark": "autotest",
                "extra_info": "extra_info",
                "image": {
                    "data": image1,
                }
            })
        resp = self.portraitmanager_createPortraits(db_id=db_id1, portrait_infos=portrait_infos)
        portrait_ids = []
        for idx, info in enumerate(resp.json_get("details")):
            self.assertEqual(info["code"], 0, "第%s个error, code=%s" % (idx, info["code"]))
            portrait_ids.append(info["portrait_id"])

        self.logTestStep('logging.conf:1对比')
        db_id = db_id1
        image = {"data": self.imageToBase64(os.path.join(self.cfg.image_dir, "xueqi1.jpg"))}
        resp = self.portraitmanager_compareImageInDB(db_id=db_id, portrait_id=portrait_ids[0], image=image)
        self.assertTrue(resp.json_get("detail.score") < 0.9)
        self.assertTrue(resp.json_get("detail.quality") > 0.9)

        resp = self.portraitmanager_compareImageInDB(db_id=db_id, portrait_id=portrait_ids[9], image=image)
        self.assertTrue(resp.json_get("detail.score") > 0.9)
        self.assertTrue(resp.json_get("detail.quality") > 0.9)

        self.logTestStep('删除人像库')
        resp = self.portraitmanager_deletePortraitDb(db_id=db_id1, unsafe_mode=True)
        self.assertEqual(resp.status_code, 200)
        self.logTestStep('再次查询人像库,确认删除成功')
        resp = self.portraitmanager_queryPortraitDbs(db_ids=[db_id1])
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.json_get("data")), 0)

    def test_nebula_portraits_imageCompare_1Vs1(self):
        self.logTestTitle("图片1:1比对")
        image_name_list = ["guojin1.jpg", "guojin2.jpg", "pengcheng1.jpg", "pengcheng2.jpg",
                           "shihan1.jpg", "shihan2.jpg", "shihan3.jpg", "shihan4.jpg", "xueqi1.jpg", "xueqi2.jpg"]
        self.logTestTitle("同一个人对比")
        image1 = {"data": self.imageToBase64(os.path.join(self.cfg.image_dir, "xueqi1.jpg"))}
        image2 = {"data": self.imageToBase64(os.path.join(self.cfg.image_dir, "xueqi2.jpg"))}
        resp = self.portraitmanager_compareOneToOne(image_1=image1, image_2=image2)
        self.assertTrue(resp.json_get("detail.score") > 0.9)
        self.assertTrue(resp.json_get("detail.quality_1") > 0.9)
        self.assertTrue(resp.json_get("detail.quality_2") > 0.9)

        self.logTestTitle("不同人对比")
        image1 = {"data": self.imageToBase64(os.path.join(self.cfg.image_dir, "guojin1.jpg"))}
        image2 = {"data": self.imageToBase64(os.path.join(self.cfg.image_dir, "xueqi2.jpg"))}
        resp = self.portraitmanager_compareOneToOne(image_1=image1, image_2=image2)
        self.assertTrue(resp.json_get("detail.score") < 0.1)
        self.assertTrue(resp.json_get("detail.quality_1") > 0.9)
        self.assertTrue(resp.json_get("detail.quality_2") > 0.9)

        self.logTestTitle("不同人对比,错误的version")
        image1 = {"data": self.imageToBase64(os.path.join(self.cfg.image_dir, "guojin1.jpg"))}
        image2 = {"data": self.imageToBase64(os.path.join(self.cfg.image_dir, "xueqi2.jpg"))}
        resp = self.portraitmanager_compareOneToOne(image_1=image1, image_2=image2, feature_version="2500")
        # TODO: 错误的version返回错误信息有bug, 目前返回500

        self.logTestTitle("错误的base64")
        image1 = {"data": "111111"}
        image2 = {"data": self.imageToBase64(os.path.join(self.cfg.image_dir, "xueqi2.jpg"))}
        resp = self.portraitmanager_compareOneToOne(image_1=image1, image_2=image2)
        self.assertTrue(resp.json_get("code") == 3)
